<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022\1\24 0024
 * Time: 17:56
 */

/**
 * 设置消息
 * @Route("set/message/notice",name="set_message_notice")
 */
public function setMessageNotice($param)
{
    $param = json_encode($param);
    $db = $this->selectDb();
    $RedisClusterController = new RedisClusterController($db);
    $RedisClusterController->rpush('message_notice', $param);
}

//选择数据库
public function selectDb()
{
    $db = 2;
    $request = Request::createFromGlobals();
    if ($request->getHost() == '10.0.11.4' || $request->getHost() == 'sheratan.repugene.com') {
        $db = 1;
    }
    return $db;
}

/**
 *
 * @Route("message/notice/inset/pgsql",name="message_notice_insert_pgsql")
 * @param Request $request
 */
public function messageNoticeInsertPgsql()
{
    $db = $this->selectDb();
    $redis = new RedisClusterController($db);
    for ($i = 1; $i < 30; $i++) {
        $this->doInset($redis);
        sleep(2);
    }
}

public function doInset($redis)
{
    $param = $redis->rpop('message_notice');
    $param = json_decode($param, true);
    $user_id = $param['user_id'];
    $type = $param['type'];
    $title = $param['title'];
    $notice_content = $param['notice_content'];
    $chain_id = $param['chain_id'];
    if ($param) {
        $message_notice = new MessageNotice();
        $message_notice->setUserId($user_id);
        $message_notice->setType($type);
        $message_notice->setCreateTime(date('Y-m-d H:i:s', time()));
        $message_notice->setTitle($title);
        $message_notice->setNoticeContent($notice_content);
        $message_notice->setIsRead(0);
        $message_notice->setChainId($chain_id);
        $this->flush($message_notice);
    }
}



/** 获取这个人的消息列表通过
 * @Route("/get/user/message/notice",name="get_user_message_notice")
 * @Method("POST")
 */
public function getUserMessageNotice(Request $request)
{
    $db = $this->selectDb($request);
    $RedisClusterController = new RedisClusterController($db);
    $user_id = $request->get('user_id');
    $data = $RedisClusterController->lrange('message_' . $user_id);
    $type = $request->get('type');
    $data = ArrayProcessController::jsonArrayToArray($data);
    //过滤掉某些类型数据
    $data = ArrayProcessController::filterData($type, $data);
    $offset = $request->get('offset');
    $count = $request->get('count');
    $postFields = array(
        'offset' => $offset,
        'count' => $count
    );
    $postFields = json_encode($postFields);
    $data = $this->getCacheInfo($data, $postFields);
    return $this->suc('成功！', $data, count($data));
}

//选择数据库
public function selectDb($request)
{
    $db = 2;
    $request = Request::createFromGlobals();
    $port = $request->getPort();
    if (in_array($port, ['3130', '9527', '3132', '87'])) {
        $db = 1;
    }
    return $db;
}

public function getCacheInfo($data, $postFields)
{
    $postFields = json_decode($postFields);
    $offset = $postFields->offset;
    $dataNew = [];
    foreach ($data as $key => $value) {
        if ($key >= $offset && $key < $offset + 20) {
            $dataNew[] = $value;
        }
    }
    return $dataNew;
}